//
//  ST_GiftAnimationViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import PINRemoteImage
import SwiftyUserDefaults

class ST_GiftAnimationViewController: UIViewController {

    deinit {
        print("SVGA play bug --- ST_GiftAnimationViewController deinit")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(scan_animate_view)
        scan_animate_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(150.0/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(iPhoneXSeries ? -80 : -10)
        }
        
        self.view.addSubview(scan_gift_image_view)
        scan_gift_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(116/375.0*ScreenWidth)
            make.center.equalTo(scan_animate_view)
        }
        
        
        scan_animate_view.play()
        
        main_thread_execute(afterDelay: 2) {
            self.goback_previous_controller(animated: false)
        }
        
    }
    
    var url = ""{
        didSet{
            scan_gift_image_view.pin_setImage(from: url.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        }
    }
    
    fileprivate lazy var scan_gift_image_view:UIImageView = {
       
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 116/2.0
        return control
    }()
    
    fileprivate lazy var scan_animate_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "receiveGiftBack", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.loopAnimation = true
        
        return control
    }()
    
    
    class func browserGift(imageUrl:String,svgaUrl:String?){
        
        if let url = svgaUrl?.url{
            
            let animateVC = ST_SVGAGiftPlayController()
            
            ST_GlobalAutoDismissDistributor.distributor.autoDismissControllerCollection.append(WeakWrapper<UIViewController>(value: animateVC))
            
            animateVC.overCurrentContext = true
            
            animateVC.over_current_context_alpha = "0.4"
            
            animateVC.svgaUrl = url
            
            GlobalPresent(toController: animateVC, animated: false)
            
        }else{
            
            let animateVC = ST_GiftAnimationViewController()
            
            ST_GlobalAutoDismissDistributor.distributor.autoDismissControllerCollection.append(WeakWrapper<UIViewController>(value: animateVC))
            
            animateVC.overCurrentContext = true
            
            animateVC.over_current_context_alpha = "0.4"
            
            animateVC.url = imageUrl
            
            GlobalPresent(toController: animateVC, animated: false)
        }
        
        
        
        
        
    }

}







//MARK: -
class MemberGoodsAnimationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(avatar_image_view)
        self.view.addSubview(icon_supplement_image_view)
        
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(115/375.0*ScreenWidth)
            make.center.equalTo(icon_supplement_image_view)
        }
        
        
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(155/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(iPhoneXSeries ? -80 : -10)
        }
        
        main_thread_execute(afterDelay: 2) {
            self.goback_previous_controller(animated: false)
        }
        
    }
    
    var param:(avatar:String,supplementUrl:String)?{
        didSet{
            
            avatar_image_view.pin_setImage(from: self.param?.avatar.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            icon_supplement_image_view.pin_setImage(from: self.param?.supplementUrl.url)
        }
    }
    
    fileprivate lazy var icon_supplement_image_view:UIImageView = {
        let control = UIImageView()
       control.contentMode = UIView.ContentMode.scaleAspectFill
       return control
    }()

    fileprivate lazy var avatar_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 116/2.0
        return control
    }()
}



//MARK: -
class ST_LoginGiftAnimationViewController: UIViewController {

    let imgUrl:String
    let awardsName:String
    
    init(centerImageUrl:String,awardsName:String){
        
        imgUrl = centerImageUrl
        self.awardsName = awardsName
        super.init(nibName: nil, bundle: nil)
        
        self.overCurrentContext = true
        self.over_current_context_alpha = "0.4"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(scan_animate_view)
        scan_animate_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(150.0*0.8/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        self.view.addSubview(scan_gift_image_view)
        scan_gift_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(scan_gift_image_view_width/375.0*ScreenWidth)
            make.center.equalTo(scan_animate_view)
        }
        
        self.view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(25)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(scan_gift_image_view.snp.top).offset(-30)
        }
        
        self.view.addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(scan_gift_image_view.snp.bottom).offset(20)
        }
        
        scan_animate_view.play()
        
        main_thread_execute(afterDelay: 2) {
            
            self.goback_previous_controller(animated: false) {
                
//                ST_FateAlertManager.shared.dispatch()
                
                SCComponets.alert_dispatcher?.nextAlert()
            }
        }
    }
    
    fileprivate let scan_gift_image_view_width:CGFloat = 116*0.5
    
    fileprivate lazy var scan_gift_image_view:UIImageView = {
       
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = scan_gift_image_view_width/2.0
        control.pin_setImage(from: imgUrl.url, placeholderImage: nil)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel()
        control.text = "获得奖励"
        control.textColor = .white
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel()
        control.attributedText = self.awardsName.withAttributes([Attribute.textColor(Color.init(hex: 0xFEC207)!),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular))])
        return control
    }()
    
    fileprivate lazy var scan_animate_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "receiveGiftBack", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.loopAnimation = true
        
        return control
    }()

}




//MARK: - Others
class ST_GlobalAutoDismissDistributor {
    
    static let distributor = ST_GlobalAutoDismissDistributor()
    
    private init(){}
    
    /// 可自动关闭的Controllers
    var autoDismissControllerCollection = [WeakWrapper<UIViewController>]()
    
}

